package com.feng.app.spider;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;

/**
 * 下载网页
 * @author feng
 *
 */
public class DownLoad{
	
	HttpClient client=HttpClients.createDefault();
	String url;
	public DownLoad(String url) {
		this.url=url;
	}
	
	/**
	 * 下载
	 * 输入url
	 */
	public void run(){
		HttpGet get=new HttpGet(url);
		
		HttpResponse response;
		try {
			
			response = client.execute(get);
			
			HttpEntity entity=response.getEntity();
			
			InputStream  is=entity.getContent();
			/////////////////////////////////////////////////////////////////
			Header header=entity.getContentType();							//
//										System.out.println(response.getFirstHeader(url).getName());															//获取名称
			String type=header.toString();												//
			type=type.substring(type.lastIndexOf("/")+1);		
			if(type.length()>4){
				type=type.substring(0, 4);
			}																								//
			/////////////////////////////////////////////////////////////////
			
			SimpleDateFormat sdf=new SimpleDateFormat("yyMMddHHmmss");
			FileOutputStream fos=new FileOutputStream(sdf.format(new Date())+"."+type);
			
			byte b[]=new byte[1024];
			int l=0;
			while((l=is.read(b))!=-1){
				fos.write(b,0,l);
			}
			
			
			fos.flush();
			
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}

}
